package com.lagou.webwork.dao;

import com.lagou.webwork.model.User;
import com.lagou.webwork.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author sjp
 * @date 2021/5/26 18:45
 * @description
 */
public class UserDaoImp implements UserDao {
    /**
     *
     * @param user
     * @return  null表示未找到
     */
    @Override
    public User login(User user) {

        try {
            //1.创建QueryRunner对象，使用Druid自动管理方式创建
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            //2.编写查询SQL
            String sql = "select * from user where name = ? and password = ?";
            User queryUser = queryRunner.query(sql, new BeanHandler<>(User.class), user.getName(), user.getPassword());

            if (null != queryUser) {
                return queryUser;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }
}
